﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace StatusPlan3.UC
{
    /// <summary>
    /// Interaction logic for UCUsers.xaml
    /// </summary>
    public partial class UCUsers : UserControl
    {
        public UCUsers()
        {
            InitializeComponent();
            populateFrame();
        }

        public void populateFrame()
        {
            Users users = StatusPlanData.statusPlanHandler.getAllUsers();
            userGridView.ItemsSource = users;
        }

        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            Window_User windowUser = new Window_User();
            windowUser.showNew();
            populateFrame();
        }

        private void userGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            User u = userGridView.SelectedItem as User;
            if (u != null)
            {
                EditButton.IsEnabled = true;
                DeleteButton.IsEnabled = true;
                ChangePasswordButton.IsEnabled = true;
            }
            else
            {
                EditButton.IsEnabled = false;
                DeleteButton.IsEnabled = false;
                ChangePasswordButton.IsEnabled = false;
            }
        }

        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            User u = userGridView.SelectedItem as User;
            if (u != null)
            {
                Window_User wu = new Window_User();
                wu.showEdit(u);
                populateFrame();
            }
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            User u = userGridView.SelectedItem as User;
            if (u != null)
            {
                StatusPlanData.statusPlanHandler.deleteUser(u);
                populateFrame();
            }
        }

        private void ChangePasswordButton_Click(object sender, RoutedEventArgs e)
        {
            User u = userGridView.SelectedItem as User;
            if (u != null && MessageBox.Show("Ert tú vís/ur at " + u.fullName + " skal broyta kotu næstuferð viðkomandi innritar?","Broyta kotu",MessageBoxButton.YesNoCancel,MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                u.changePassword = true;
                StatusPlanData.statusPlanHandler.saveUser(u);
                populateFrame();
            }
        }
    }
}
